package com.digital.smartgrow.controller.sysinfo;

import com.digital.smartgrow.common.advice.Result;
import com.digital.smartgrow.dto.datacenter.DictionaryPageDTO;
import com.digital.smartgrow.entity.sysinfo.Dictionary;
import com.digital.smartgrow.service.sysinfo.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 字典 前端控制器
 * </p>
 *
 * @author sunyuechao
 * @since 2022-04-23
 * 修改
 */
@Api(tags = "字典")
@RestController
@RequestMapping("${context.prefix}/dict")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    @ApiOperation(value = "列表分页查询")
    @PostMapping("/list")
    public Object getListByPage(@RequestBody DictionaryPageDTO dictionaryPageDTO){
        return dictionaryService.getListByPage(dictionaryPageDTO);
    }

    @ApiOperation(value = "字典详情")
    @GetMapping("/{id}")
    public Object getInfo(@PathVariable Integer id){
        if (id == null) return "id不能为空";
        return dictionaryService.getInfo(id);
    }


    @ApiOperation(value = "字典类型-删除")
    @DeleteMapping("/{id}")
    public Object removeById(@PathVariable Integer id) {
        if (id == null) return Result.fail("id不能为空");
        Dictionary dictionary = dictionaryService.getById(id);
        if(dictionary.getIssys()==1){
            return Result.fail("系统字典不允许删除");
        }
        return dictionaryService.removeById(id);
    }

    @ApiOperation(value = "字典类型-新增")
    @PostMapping()
    public Object add(@Valid @RequestBody Dictionary dictionary) {

        return dictionaryService.save(dictionary);
    }

    @ApiOperation(value = "字典类型-编辑")
    @PutMapping()
    public Object edit(@Valid @RequestBody Dictionary dictionary) {

        if (dictionary == null || dictionary.getId() == null) return Result.fail("id不能为空");
        if(dictionary.getIssys()==1){
            return Result.fail("系统字典不允许编辑");
        }
        return dictionaryService.updateById(dictionary);
    }


}
